package com.sofaguest.service;

import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.sofaguest.dao.LogDao;
import com.sofaguest.dao.ManagerDao;
import com.sofaguest.entry.LogInfo;
import com.sofaguest.entry.SofaResult;
import com.sofaguest.utils.SofaUtils;

/**
 * 登录操作的业务处理类
 */
@Service
// 追加事务管理
@Transactional
public class LoginServiceImpl implements LoginService {

    Logger log = Logger.getLogger("LoginServiceImpl");

    @Resource
    private LogDao logDao;
    @Resource
    private ManagerDao managerDao;

    @Override
    public SofaResult login(String userName, String passWord) {
        log.info("登陆:" + userName + "," + passWord);
        LogInfo user = logDao.login(userName);
        if (SofaUtils.isEmpty(user)) {
            return new SofaResult(SofaResult.ERROR_CODE, "用户名或密码错误", "");
        }
        if (!user.getPass_word().equals(passWord)) {
            return new SofaResult(SofaResult.ERROR_CODE, "用户名或密码错误", "");
        }
        return new SofaResult(SofaResult.SUCCESS_CODE, "登陆成功", user);
    }

    @Override
    public SofaResult register(String userId, String pwd, String realName, String sex, String certNum, String linkTel,
            String mail, String userType) {
        log.info("注册:" + userId + "," + pwd + "," + realName + "," + sex + "," + certNum + "," + linkTel + "," + mail
                + "," + userType);
        LogInfo user = logDao.login(userId);
        if (!SofaUtils.isEmpty(user)) {
            return new SofaResult(SofaResult.ERROR_CODE, "该用户名已被注册!", "");
        }
        // 注册时间
        String joinTime = SofaUtils.getSysDate();
        String validFlag = "1"; // 默认数据生效
        String unUseTime = null; // 数据失效时间默认为空
        LogInfo newUser = new LogInfo(userId, pwd, realName, sex, certNum, linkTel, mail, userType, joinTime,
                validFlag, unUseTime, "", "");
        logDao.newUser(newUser);
        return new SofaResult(SofaResult.SUCCESS_CODE, "注册成功!请牢记密码", "");
    }

    @Override
    public SofaResult modifyUserInfo(String userId, String certNum, String sex, String linkTel, String mail, String pwd) {
        if (SofaUtils.isEmpty(userId)) {
            return new SofaResult(SofaResult.ERROR_CODE, "登录状态已失效,请重新登录", "");
        }
        LogInfo user = new LogInfo();
        user.setUser_id(userId);
        if (!"null".equals(certNum)) {
            user.setCert_num(certNum);
        }
        if (!"null".equals(sex)) {
            user.setSex(sex);
        }
        if (!"null".equals(linkTel)) {
            user.setTel_phone(linkTel);
        }
        if (!"null".equals(mail)) {
            user.setEmail(mail);
        }
        if (!"null".equals(pwd)) {
            user.setPass_word(pwd);
        }
        logDao.modifyUserInfo(user);
        return new SofaResult(SofaResult.SUCCESS_CODE, "修改成功", "");
    }

    @Override
    public SofaResult modifyPwd(String userId, String oldPwd, String newPwd) {
        LogInfo user = logDao.login(userId);
        if (SofaUtils.isEmpty(user) || !user.getPass_word().equals(oldPwd)) {
            return new SofaResult(SofaResult.ERROR_CODE, "原始密码错误,请重试!", "");
        }
        if (!oldPwd.equals(newPwd)) {
            LogInfo modifyUser = new LogInfo();
            modifyUser.setUser_id(userId);
            modifyUser.setPass_word(newPwd);
            logDao.modifyPwd(modifyUser);
        }
        return new SofaResult(SofaResult.SUCCESS_CODE, "密码修改成功,请牢记新密码,重新登录后使用", "");
    }

    @Override
    public SofaResult qryUser(String byTimeTag) {
        List<LogInfo> users = logDao.qryUser(Integer.valueOf(byTimeTag));
        return new SofaResult(SofaResult.SUCCESS_CODE, "查询成功", users);
    }

    @Override
    public SofaResult managerInfo(String id, String type, String acctId) {
        String unUseTime = SofaUtils.getSysDate();
        Map dataMap = SofaUtils.asMap("id", id, "unUseTime", unUseTime, "reason", "管理员操作删除", "operId", acctId);
        if ("user".equals(type)) {
            managerDao.deleteUser(dataMap);
        }
        else if ("sofa".equals(type)) {
            managerDao.deleteSofa(dataMap);
        }
        else if ("order".equals(type)) {
            managerDao.deleteOrder(dataMap);
        }
        else if ("msg".equals(type)) {
            managerDao.deleteMsg(dataMap);
        }
        else if ("travel".equals(type)) {
            managerDao.deleteTravel(dataMap);
        }
        else {
            return new SofaResult(SofaResult.ERROR_CODE, "删除操作的类型有误,类型:" + type, "");
        }
        Map typeMap = SofaUtils.asMap("user", "用户", "sofa", "沙发", "order", "订单", "msg", "留言", "travel", "游记");
        return new SofaResult(SofaResult.SUCCESS_CODE, typeMap.get(type) + "信息删除完成", "");
    }
}
